package com.egghiro.framework.j2se.game.map.maze.creator;

import com.egghiro.framework.j2se.game.map.maze.cons.MazeConst;
import com.egghiro.framework.j2se.game.map.maze.exception.MazeSizeException;

public abstract class MazeCreator {
    protected int[][] mazeArray;

    public final int[][] createMaze(int xSize, int ySize) throws MazeSizeException {
        if (checkSize(xSize, ySize)) {
            this.mazeArray = new int[xSize][ySize];
            createMaze();
            return mazeArray;
        } else {
            throw new MazeSizeException(getMazeSizeExceptionMessage());
        }
    }

    protected String getMazeSizeExceptionMessage() {
        return "";
    }

    protected boolean checkSize(int xSize, int ySize) {
        if (xSize > 4 && ySize > 4) {
            return true;
        } else {
            return false;
        }
    }

    protected abstract void createMaze();

    protected final void makeAll(final int MAKE_TO) {
        for (int i = 0; i < mazeArray.length; i++) {
            for (int j = 0; j < mazeArray[i].length; j++) {
                mazeArray[i][j] = MAKE_TO;
            }
        }
    }

    protected final void makeAllWall() {
        this.makeAll(MazeConst.WALL);
    }

    protected final void makeAllRoad() {
        this.makeAll(MazeConst.RODE);
    }

    protected final void makeOuter(final int MAKE_TO) {
        // 上
        for (int i = 0; i < mazeArray[0].length; i++) {
            mazeArray[0][i] = MAKE_TO;
        }

        // 下
        final int MAX_Y_INDEX = mazeArray.length - 1;
        for (int i = 0; i < mazeArray[MAX_Y_INDEX].length; i++) {
            mazeArray[MAX_Y_INDEX][i] = MAKE_TO;
        }

        // 左
        for (int i = 0; i < mazeArray.length; i++) {
            mazeArray[i][0] = MAKE_TO;
        }

        // 右
        final int MAX_X_INDEX = mazeArray[0].length - 1;
        for (int i = 0; i < mazeArray.length; i++) {
            mazeArray[i][MAX_X_INDEX] = MAKE_TO;
        }
    }

    protected final void makeOuterWall() {
        this.makeOuter(MazeConst.WALL);
    }

    protected final void makeOuterRoad() {
        this.makeOuter(MazeConst.RODE);
    }

}
